package br.com.foxinline.manager.pesquisar;

import br.com.foxinline.modelo.Distribuicao;
import br.com.foxinline.modelo.Selo;
import br.com.foxinline.modelo.TipoSelo;
import br.com.foxinline.modelo.Usuario;
import br.com.foxinline.servico.SeloServico;
import br.com.foxinline.servico.TipoSeloServico;
import br.com.foxinline.servico.UsuarioServico;
import java.io.IOException;
import java.io.Serializable;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

/**
 *
 * @author cadomeireles
 */
@ViewScoped
@ManagedBean
public class ManagerPesquisarSelo implements Serializable {

    @EJB
    private SeloServico seloServico;
    @EJB
    private TipoSeloServico tipoSeloServico;
    @EJB UsuarioServico usuarioServico;
    
    private Selo selo;
    private List<Selo> selos;
    private Selo seloVisualizar;
    
    private String seloId;
    
    @PostConstruct
    public void init() {
        instanciar();
        delegar();
    }

    private void instanciar() {
        this.selo = new Selo();
        this.selos = new ArrayList<Selo>();
        this.seloVisualizar = new Selo();
    }

    private void delegar() {
        Map<String, String> parametros = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        this.seloId = parametros.get("seloId");

        if (this.seloId != null) {
            this.seloVisualizar = seloServico.pesquisar(Long.parseLong(this.seloId));
        }

    }

    public void pesquisar() {
        this.selos = seloServico.pesquisar(this.selo);
    }

    public void limpar() {
        instanciar();
    }

    public void cancelar() {
        this.seloVisualizar.setAtivo(false);
        this.seloVisualizar.setCancelado(true);
        this.seloVisualizar.setDataCancelamento(new Date());
        
        Principal userPrincipal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
        String userPrincipalName = userPrincipal.getName();
        Usuario usuario = usuarioServico.getUsuarioPorLogin(userPrincipalName);
        this.seloVisualizar.setUsuarioCancelamento(usuario);
        
        seloServico.atualizar(this.seloVisualizar);
        
        ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
        FacesContext fc = FacesContext.getCurrentInstance();
        FacesContext.getCurrentInstance().addMessage("messages", new FacesMessage(FacesMessage.SEVERITY_INFO, "Selo cancelado com sucesso!", ""));
        fc.getExternalContext().getFlash().setKeepMessages(true);

        try {
            context.redirect("pesquisarSelo.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    public List<TipoSelo> autocompletarTipoSelo(String query) {
        return tipoSeloServico.autocompletar(query);
    }    

    public Selo getSelo() {
        return selo;
    }

    public void setSelo(Selo selo) {
        this.selo = selo;
    }

    public List<Selo> getSelos() {
        return selos;
    }

    public void setSelos(List<Selo> selos) {
        this.selos = selos;
    }

    public Selo getSeloVisualizar() {
        return seloVisualizar;
    }

    public void setSeloVisualizar(Selo seloVisualizar) {
        this.seloVisualizar = seloVisualizar;
    }

}
